package com.huopaolan.yohuoshop.Mvp.View.Activity;

import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.huopaolan.yohuoshop.Dao.Service.DBService;
import com.huopaolan.yohuoshop.Mvc.Model.QrCodeUtil;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.huopaolan.yohuoshop.Sp.SpUtils;

public class QrCodeActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_ZXing_back, iv_ZXing_qr_code;
    private SimpleDraweeView iv_ZXing_user_head;
    private TextView tv_ZXing_user_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_qr_code);
        initView();
        initDate();
    }

    @Override
    protected void onStart() {
        Window window = getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    private void initDate() {
        String spReadUser = SpUtils.getInstance().SpReadUser();
        Cursor user = DBService.getDbService().query("User", "user_id=?", new String[]{spReadUser});
        while (user.moveToNext()) {
            String user_id = user.getString(user.getColumnIndex("user_id"));
            String user_names = user.getString(user.getColumnIndex("nick_name"));
            String user_heads = user.getString(user.getColumnIndex("user_head"));
            Bitmap qrCode = QrCodeUtil.createQRCode(user_id);
            iv_ZXing_qr_code.setImageBitmap(qrCode);
            tv_ZXing_user_name.setText("" + user_names);
            Uri uri = Uri.parse(API.BaseUrl + user_heads);
            ImagePipeline imagePipeline = Fresco.getImagePipeline();
            imagePipeline.evictFromMemoryCache(uri);
            imagePipeline.evictFromDiskCache(uri);
            imagePipeline.evictFromDiskCache(uri);
            iv_ZXing_user_head.setImageURI(uri);
        }
    }

    private void initView() {
        tv_ZXing_user_name = findViewById(R.id.tv_ZXing_user_name);
        iv_ZXing_user_head = findViewById(R.id.iv_ZXing_user_head);
        iv_ZXing_qr_code = findViewById(R.id.iv_ZXing_qr_code);
        iv_ZXing_back = findViewById(R.id.iv_ZXing_back);
        iv_ZXing_back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_ZXing_back:
                finish();
                break;
        }
    }
}
